home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Demos / Extend 3.0 Demo / Demo Libraries / Demo Generic Lib / Demo Generic Lib.rsrc / MODL_17409_Conversion Table < prev    next >
Encoding:
Text File  |  1994-06-22  |  6.5 KB  |  294 lines

  1. real dataonly[][2];
  2. real xdata[],ydata[];
  3. real dataMax, dataMin;    ** corrected for roundoff error
  4. integer maxSize;        ** maximum size of data table (see createBlock msg)
  5. integer max;            ** length of data array
  6. integer x,y;
  7. real tempx,tempy, xvalue, yvalue;
  8. double    timeArray[];
  9.  
  10. ** This block calculates an output from an input based on a table.
  11. ** Copyright © 1989-1994 by Imagine That, Inc.
  12. **    All Rights Reserved.
  13. ** Extend Generic Library, Conversion Table block; Alfy Riddle
  14. **        modified     3/1/92  JSL for V2.0
  15. **                    8/18/93 JSL pass noValues through
  16. **                    3/10/94 DJK modified report
  17. **                    3/10/94 DJK added block label to trace & report
  18. **                    5/4/94    DJK modified checkdata so that using row 500 would not cause an out of bounds error
  19. **
  20. ** The sort fcn of this block came from the:
  21. ** Nonlinear Function Generator 2/20/89
  22. ** Authors: Cheryl Blanford and Chuck Oman
  23. ** MIT Man Vehicle Laboratory
  24. ** Rm 37-219, Cambridge, MA 02139
  25.  
  26.  
  27. procedure getValue()
  28. {
  29.     integer lastX,notDone;
  30.     
  31.     if (noValue(xIn))
  32.         {
  33.         yOut = xIn;
  34.         return;
  35.         }
  36.  
  37.     lastX = 0;        ** this is a starting point for the interpolation
  38.                     ** it keeps track of the last interpolated point
  39.     
  40.     **  use the input and interpolate to find the y value.
  41.     xvalue = xin;
  42.     if(xvalue<dataMin || xvalue>dataMax)    ** avoids roundoff error msg first pt
  43.         {
  44.         if( stop )
  45.             {    ** show input
  46.             usererror("Conversion Table input ("+xvalue+") outside valid range in block number "+(MyBlockNumber()));
  47.             abort;
  48.             }
  49.         else
  50.             yOut = 0.0;
  51.         }
  52.     else
  53.         {
  54.         for x=0 to (max-1)
  55.             {            
  56.             if (xvalue == data[x][0])
  57.                 {
  58.                 yout = data[x][1];
  59.                 break;
  60.                 }
  61.                 
  62.             if (xvalue < data[x][0])
  63.                 {
  64.                 if( interp )        ** interpolate
  65.                     {
  66.                     yvalue = ((xvalue - data[x-1][0])*(data[x][1] - 
  67.                           data[x-1][1])/(data[x][0] - data[x-1][0])) 
  68.                           + data[x-1][1];
  69.                       }
  70.                   else                **    stepped
  71.                       {
  72.                       yValue = data[x-1][1];
  73.                       }
  74.                    yOut = yValue;
  75.                 break;
  76.                 }      
  77.             }    ** for loop
  78.         }
  79.  
  80.     ** sysGlobal2 is the file reference number for the DEBUG TRACE
  81.     if( sysGlobal2 != 0.0 ) ** 0 is error, check for open file for TRACE
  82.         {
  83. // template for report:      |BLOCK NAME *****************|block number |BLOCK NUMBER*******
  84.         fileWrite(sysGlobal2,"Conversion Table             block number "+(MyBlockNumber())+".  Current Time:"+currentTime+".","",True);
  85.         if(getBlockLabel(myBlockNumber()) != "")
  86.             fileWrite(sysGlobal2,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
  87.         fileWrite(sysGlobal2,"     Input = "+xIn,"",True);
  88.         fileWrite(sysGlobal2,"     Output = "+yOut,"",True);
  89.         fileWrite(sysGlobal2," ","",True);
  90.         }
  91. }
  92.  
  93.  
  94. Procedure validMax()
  95. {
  96.     integer i;
  97.     i=0;
  98.     
  99.     while( i<maxSize && !noValue(data[i][0]) )
  100.         {
  101.         i++;
  102.         }
  103.     max = i;
  104.  
  105.     if (max <= 1)    ** check for at least 2 rows
  106.         {
  107.         usererror("Must have at least two rows of data in Conversion \
  108. Table block number "+MyBlockNumber());
  109.         abort;
  110.         }
  111. }
  112.  
  113.  
  114. Procedure sortFcn()
  115. {
  116.     validMax();
  117.     
  118.     ** split data into 2 arrays for ease of sorting.    
  119.     makearray(dataonly,max);
  120.     makearray(xdata,max);
  121.     makearray(ydata,max);                
  122.  
  123.     for (x=0;x<max;x++)
  124.           {
  125.           dataonly[x][0] = data[x][0];
  126.            dataonly[x][1] = data[x][1];
  127.            }
  128.  
  129.     for(x = 0 ;x<max;x++)
  130.          {
  131.          xdata[x] = data[x][0];
  132.           ydata[x] = data[x][1];
  133.           }
  134.     
  135.     for (x=0;x<max;x++)
  136.           for (y=x+1;y<max;y++)
  137.             if(xdata[y] < xdata[x])
  138.                 {
  139.                  tempx = xdata[y];
  140.                  tempy = ydata[y];
  141.                  xdata[y] = xdata[x];
  142.                  ydata[y] = ydata[x];
  143.                  xdata[x] = tempx;
  144.                  ydata[x] = tempy;
  145.                 }
  146.  
  147.  
  148.     **restore table, now properly sorted.
  149.     for (x = 0;x<max;x++ )
  150.         {
  151.         data[x][0] = xdata[x];
  152.           data[x][1] = ydata[x];
  153.           }
  154. }
  155.  
  156.  
  157. on choosetoplot
  158. {
  159.     integer k, numPlot;
  160.     validMax();
  161.  
  162.             ** support stepped plots
  163.     if( interp )
  164.         numPlot = max;
  165.     else if( stepped )
  166.         numPlot = 2 * max - 1;
  167.  
  168.     makearray(xdata,numPlot);
  169.     makearray(ydata,numPlot);                
  170.     
  171.     for (k=0;k<max;k++)
  172.         {
  173.         if( interp )
  174.             {
  175.             xdata[k] = data[k][0];
  176.             ydata[k] = data[k][1];
  177.             }
  178.         else    ** stepped
  179.             {
  180.             xdata[2*k] = data[k][0];
  181.             ydata[2*k] = data[k][1];
  182.             
  183.             if( k < max-1 )    ** odd number of points (skip last)
  184.                 {
  185.                 xdata[2*k+1] = data[k+1][0];    ** x coordinates
  186.                 ydata[2*k+1] = data[k][1];
  187.                 }
  188.             }
  189.         }
  190.     
  191.     ** install the axes
  192.     installAxis(0, "Conversion Table Curve",
  193.          "X In", FALSE, 0,20,
  194.             "Y Out", FALSE, 0,20, "", 0, 0, 0, 
  195.             blackpattern, blackcolor, 100);
  196.     installArray(0, 0, "X In", xdata, 0, 20, 
  197.                     0, 0, blackPattern, cyanColor);
  198.     installArray(0, 1, "Y Out", ydata, 0, 20,
  199.                     0, 0, dkgrayPattern, redColor);
  200.  
  201.     makeScatter(0, 0);    
  202.     ** plot the data
  203.  
  204.       for (k=0;k<numPlot;k++)
  205.           plotNewScatter(0,0,k,xdata[k],ydata[k]);
  206.     
  207.     autoscalex(0);
  208.     autoscaley(0);
  209.     showPlot(0,"Conversion Table Curve");
  210. }        
  211.  
  212.  
  213. ** This message occurs for each step in the simulation.
  214. on simulate
  215. {    
  216.     getValue();
  217. }
  218.  
  219.  
  220. on endsim
  221. {
  222.     integer i;
  223.     
  224.     ** sysGlobal1 is the file reference number for the REPORT
  225.     if( sysGlobal1 != 0.0 )  ** 0 is error, check for open file for REPORT
  226.         {
  227. // template for report:      |BLOCK NAME *****************|block number |BLOCK NUMBER*******
  228.         fileWrite(sysGlobal1,"Conversion Table             block number "+MyBlockNumber(),"",True);
  229.         if(getBlockLabel(myBlockNumber()) != "")
  230.             fileWrite(sysGlobal1,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
  231.         if( interp )
  232.             fileWrite(sysGlobal1,"     Interpolated","",True);
  233.         else
  234.             fileWrite(sysGlobal1,"     Stepped","",True);
  235.         if( stop )
  236.             fileWrite(sysGlobal1,"     Stops when out of range","",True);
  237.         else
  238.             fileWrite(sysGlobal1,"     Sets output to zero when out of range","",True);
  239.         fileWrite(sysGlobal1,"     Table x        y","",TRUE);
  240.         
  241.         validMax();
  242.         
  243.         for( i=0;i<max;i++ )    ** picks up old itemNum
  244.             fileWrite(sysGlobal1,"     "+RealToStr(data[i][0],8)+"      "+RealToStr(data[i][1],8),"",TRUE);
  245.         if( comments != "" )
  246.             fileWrite(sysGlobal1,"     Comments = "+comments,"",True);        
  247.         fileWrite(sysGlobal1," ","",True);
  248.         }
  249. }
  250.  
  251.  
  252. on createBlock
  253. {
  254.     maxSize = 500;
  255.     interp = 1;
  256.     stepped = 0;
  257.     stop = 1;
  258.     zero = 0;
  259. }
  260.  
  261.  
  262. on checkdata
  263. {
  264.     sysGlobal1 = 0.0;    ** prevent false reports
  265.     sysGlobal2 = 0.0;    ** prevent false debugs
  266.  
  267.     validMax();        ** check for at least 2 rows
  268. }
  269.  
  270.  
  271. ** Initialize any simulation variables.
  272. on initsim
  273. {
  274.     sortFcn();        ** sort the table
  275.  
  276.     if( data[max-1][0] > 0 )        ** for roundoff error
  277.         dataMax = data[max-1][0]*1.000000001;
  278.     else
  279.         dataMax = data[max-1][0]*0.999999999;
  280.  
  281.     if( data[0][0] > 0 )
  282.         dataMin = data[0][0]*0.999999999;
  283.     else
  284.         dataMin = data[0][0]*1.000000001;
  285.  
  286.     if( getPassedArray(sysGlobal0, timeArray) )
  287.         getSimulateMsgs(FALSE);
  288. }
  289.  
  290.  
  291. on sort
  292. {
  293.     sortFcn();    ** then sort the table
  294. }